home *** CD-ROM | disk | FTP | other *** search
/ Netware Super Library / Netware Super Library.iso / mis_util / addcrlf / acrlf.c next >
Encoding:
C/C++ Source or Header  |  1990-01-23  |  3.6 KB  |  115 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys\types.h>
  5. #include <sys\stat.h>
  6. #include <io.h>
  7.  
  8. #define BUFSIZE 1024
  9.  
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.     int fh1,fh2,result;
  15.     char buf[BUFSIZE],*cp;
  16.     unsigned int bytesread,byteswritten,nbytes,nrecs,srec,pass=0;
  17.     size_t ln;
  18.  
  19.     if (argc < 4)   /* Do we have enough information */
  20.     {
  21.         fprintf(stdout,"Usage: ACRLF Input_file Output_file Record_size [Start record] [Record quantity]");
  22.         exit(1);
  23.     }
  24.     fprintf(stdout,"ADDCRLF - Copyright (c) Glenn M. Belton 1990 - All Rights Reserved\n (703) 670-5535\n");
  25.  
  26.     fh1 = open(argv[1],O_RDONLY|O_BINARY);      /* Open the Input file */
  27.     if (fh1 == -1)
  28.     {
  29.         perror("Unable to Open Input file\n");
  30.         exit(1);
  31.     }
  32.     if((ln = lseek(fh1,0L,SEEK_END)) == -1L)    /* How Big is the File */
  33.     {
  34.         perror("Error seeking end of file");
  35.         exit(1);
  36.     }
  37.  
  38.     /* Open the Output file */
  39.     fh2 = open(argv[2],O_WRONLY|O_CREAT|O_EXCL|O_BINARY,S_IREAD|S_IWRITE);
  40.     if (fh2 == -1)
  41.     {
  42.         perror("Error opening output file");
  43.         exit(1);
  44.     }
  45.     nbytes = (unsigned) atoi(argv[3]);          /* How Big are the records */
  46.  
  47.     if (nbytes > ln)                            /* Lets compare sizes */
  48.     {
  49.         fprintf(stderr,"Record size exceeds file size");
  50.         exit(1);
  51.     }
  52.  
  53.     if (argc >= 5)                              /* Start at specific record? */
  54.     {
  55.         srec = (unsigned) atoi(argv[4]);
  56.         if (srec == 0)
  57.             srec = 1;
  58.  
  59.         if (argc > 5)                           /* Quantity? */
  60.             nrecs = (unsigned) atoi(argv[5]);
  61.         else
  62.             nrecs = 0;                           /* No Quantity, then all */
  63.  
  64.         if (((nrecs + srec)*nbytes) > ln)    /* Exceeds file size? */
  65.         {
  66.             fprintf(stderr,"Your parameters would exceed the file size\n");
  67.             exit(1);
  68.         }
  69.         else                                    /* Jump to the start position */
  70.         {
  71.             if((ln = lseek(fh1,(srec-1)*nbytes,SEEK_SET)) == -1L)
  72.             {
  73.                 perror("Error seeking record offset");
  74.                 exit(1);
  75.             }
  76.         }
  77.     }
  78.     else
  79.     {
  80.         nrecs = 0;
  81.         if((ln = lseek(fh1,0L,SEEK_SET)) == -1L)    /* Lets Rewind the input file */
  82.         {
  83.             perror("Error seeking begining of file");
  84.             exit(1);
  85.         }
  86.     }
  87.  
  88.     while ((bytesread = read(fh1,buf,nbytes)) > 0) /* Read records in a loop */
  89.     {
  90.         if ((byteswritten = write(fh2,buf,bytesread)) == -1) /* write a record */
  91.             perror("Error writing output file");
  92.         if ((byteswritten =write(fh2,"\r\n",2)) == -1)       /* append CR LF */
  93.             perror("Error writing output file");
  94.         ++pass;
  95.         if (nrecs)                              /* Specific quantity? */
  96.             if (pass == nrecs)                  /* Got them? */
  97.                 break;
  98.     }
  99.     if (bytesread < 0)                          /* Error while reading file? */
  100.     {
  101.         perror("Error reading input file");
  102.         exit(1);
  103.     }
  104.     else                                        /* No error we're at the EOF */
  105.     {
  106.         fprintf(stdout,"%u Records written",pass);
  107.         result = close(fh1);                    /* Close the input file */
  108.         if (result)
  109.             perror("Error closing input file");
  110.         result = close(fh2);                    /* Close the output file */
  111.         if (result)
  112.             perror("Error closing output file");
  113.     }
  114. }   /* Thats all Folks */
  115.